package com.yq.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yq.reggie.dto.SetmealDto;
import com.yq.reggie.pojo.Category;
import com.yq.reggie.pojo.Dish;
import com.yq.reggie.pojo.Setmeal;
import com.yq.reggie.pojo.SetmealDish;
import com.yq.reggie.service.CategoryService;
import com.yq.reggie.service.DishService;
import com.yq.reggie.service.SetmealDishService;
import com.yq.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;

    /**
     * 添加套餐和套餐菜品
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value="setmealCache",key = "#setmealDto.categoryId")
    public Result addSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.addWithSetmealDish(setmealDto);
        return Result.success("套餐添加成功");
    }

    /**
     * 套餐分页展示
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result selectPage(Integer page,Integer pageSize,String name){
        //分页对象
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);
        //条件构造对象
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        //分页查询
        setmealService.page(pageInfo,queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            setmealDto.setCategoryName(categoryService.getById(item.getCategoryId()).getName());
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(list);
        return Result.success(setmealDtoPage);
    }

    /**
     * 删除或批量删除 套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value="setmealCache",allEntries=true)
    public Result delete(@RequestParam List<Long> ids){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        long count = setmealService.count(queryWrapper);
        if(count!=0){
            return Result.fail("套餐在售，无法删除");
        }
        //批量删除套餐
        setmealService.removeBatchByIds(ids);
        return Result.success("套餐删除成功");
    }

    /**
     * 批量修改或修改 状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value="setmealCache",allEntries=true)
    public Result modifyStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        List<Setmeal> list = ids.stream().map((item) -> {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(item);
            setmeal.setStatus(status);
            return setmeal;
        }).collect(Collectors.toList());
        setmealService.updateBatchById(list);
        return Result.success("状态修改成功");
    }

    /**
     * 修改套餐：页面回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result modify_displayWithSetmealDish(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.modify_displayWithSetmealDish(id);
        return Result.success(setmealDto);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    @CacheEvict(value="setmealCache",allEntries=true)
    public Result modify(@RequestBody SetmealDto setmealDto){
        setmealService.modifyWithSetmealDish(setmealDto);
        return Result.success("修改套餐成功");
    }

    /**
     * 手机端页面展示：套餐展示
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value="setmealCache" ,key="#setmeal.categoryId")
    public Result selectByCategoryId(Setmeal setmeal){
        //根据categoryId查询setmeal表
        //sql：select * from setmeal where category_id=?,status=? order by desc update_time;
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 手机端套餐详细信息展示
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    @Cacheable(value="setmealCache" ,key="#id")
    public Result select(@PathVariable Long id){
        //跟据setmeal的id查询setmeal_dish表
        Setmeal setmeal = setmealService.getById(id);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        list.stream().map((item)->{
            LambdaQueryWrapper<Dish> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Dish::getId,item.getDishId());
            Dish dish = dishService.getOne(queryWrapper1);
            item.setImage(dish.getImage());
            return item;
        }).collect(Collectors.toList());
        return Result.success(list);
    }
}
